//限制字数
$("#signature").keyup(function(){
   var len = $(this).val().length;
   if(len > 199){
    $(this).val($(this).val().substring(0,200));
   }
});
$("#leveling_name").keyup(function(){
   var len = $(this).val().length;
   if(len > 9){
    $(this).val($(this).val().substring(0,10));
   }
});
(function(){
	function getinfo(){
		var user = JSON.parse(sessionStorage.getItem("backUserInfo"));
		$("#id").val(user.id)
		$.ajax({
			// url:'http://39.106.145.173:8089/leveling/getLevelingInfo.json',
			url:httpOrigin + '/leveling/getLevelingInfo.json',
			

			type:'POST',
			async:true,
			data:{
				id:user.id,
			},
			dataType: "json",
			success: function(data){
				if(data.code === 200 ){
					$("#leveling_name").val(data.t.levelingName)
					$("#leveling_type").val(data.t.levelingType)
					$("#photoCover1").val(data.t.levelingHead)
					$(".imgfile1").html("<img src='"+data.t.levelingHead+"'>")
					$("#gender").val(data.t.gender)
					$("#age").val(data.t.age)
					var address=data.t.address.split("-")
					$("#distpicker #provice").val(address[0])
					$("#distpicker #provice").trigger("change"); 
					$("#distpicker #city").val(address[1])
					$("#signature").val(data.t.signature)
					$(".imgfilelist").html("");
					// var a=data.t.games;
					var a=data.t.gameInfos;
					if(a.length>0){
					  var game=""
					  for(var i=0;i<a.length;i++){
						  var g=a[i].game_type==1?"王者荣耀":"绝地求生"
						  game+=g+" "
					  }
					  $(".game").html(game);	
					}else{
					  $(".game").html("无");	
					}
				    for(var i=0;i<data.t.pics.length;i++){
					   $(".imgfilelist").append("<img src='"+data.t.pics[i].picture+"' class='deleteimg'/>")	
				    }
				}else{
					alert(data.msg)	
				}
			}
		 });
	}
	var Page = {
		init:function(){
			this.render();
			this.event();
		},
		//绑定事件
		event:function(){
			//图片上传
			function fileupload(input,img,photoCover){
				var fileObj = document.getElementById(input).files[0];
				$.ajax({
					url:'http://39.106.145.173:8087/leveling/getToken.json',
					type:'POST',
					async:true,
					data:{
					},
					dataType: "json",
					success: function(data){
						var form = new FormData;
						form.append("file",fileObj);
						form.append("key",new Date().getTime()+".jpg");
						form.append("token",data.t.token);
						var domian=data.t.domain;
						$.ajax({
							url:'http://up.qiniu.com',
							type: 'POST',
							data:form,
							async: false,
							cache: false,
							contentType: false,
							processData: false,
							dataType: "json",
							success: function(data) {
							   $("."+img).append("<img src='"+domian+"/"+data.key+"' class='deleteimg'/>")	
							   $("#"+photoCover).val(""+domian+"/"+data.key+"");
							},
						});	
					}
				});				
			}
			$("#lefilelist").change(
			  function(){
				 if($(".deleteimg").length>4){alert("最多传5张图片");return}
				 fileupload("lefilelist","imgfilelist","photoCover2")
				 $("#lefilelist").val("");;
			  }
			)
			//删除图片
			$(".content-wrapper").on("click", '.deleteimg', function() {
				if (confirm("确认删除图片吗？")==true){ 
				  $(this).remove()
				}else{ 
				  return false; 
				} 
			})
			//保存按钮
			$(".btnsave").click(function(){
			  var id=$("#id").val()
			  var leveling_name=$("#leveling_name").val()
			  var leveling_head=$("#photoCover1").val()
			  var gender=$("#gender").val()
			  var age=$("#age").val()
			  var address=$("#distpicker #provice").val()+"-"+$("#distpicker #city").val()
			  var signature=$("#signature").val()
			  var LesPic="";
			  var img=$(".deleteimg");
			   if(img.length>0){
			     for(var i=0;i<img.length;i++){
				    var a="{'picUrl':'"+img[i].src+"'},"
				    LesPic+=a
			     }	 
			   }
			  LesPic=LesPic==""?null:"["+LesPic+"]";
			  
			  $.ajax({
						url:'http://39.106.145.173:8089/leveling/updateLeveling.json',
						type:'POST',
						async:true,
						data:{
							id:id,
							leveling_name:leveling_name,
							leveling_head:leveling_head,
							gender:gender,
							age:age,
							address:address,
							signature:signature,
							levelingPic:LesPic
						},
						dataType: "json",
						success: function(data){
							if(data.code === 200 ){
								alert("保存成功");
							    window.location.reload()
							}else{
								alert(data.msg)	
							}
						}
				     });
			})
		},
		//渲染页面
		render:function(){
			getinfo()
		}
	};
	Page.init();
})();